package yonghe
{
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Screen;
	import flash.display.Bitmap;
	import flash.display.Stage;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	public class Window2 extends Sprite
	{
		private static var _main:Window2;
		private static function get main():Window2
		{
			if(_main==null)
				_main = new Window2();
			return _main;
		}
		public static function get _stage():Stage
		{
			return main.newWindow.stage;
		}
		public function Window2()
		{
			_main = this;
			var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
			windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
			windowOptions.type = NativeWindowType.NORMAL;
			newWindow = new NativeWindow(windowOptions);
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.align = StageAlign.TOP_LEFT;
		}

		public var newWindow:NativeWindow;
		public static function show():void
		{
			main.newWindow.activate();
			if(Screen.screens.length > 1){
				var s:String = "right";
				if(Datas.jsonObject)
					s = Datas.jsonObject.Window2;
				var screen2pos:String = "right";
				if(s)
				{
					screen2pos = s;
				}
				//logs.adds(screen2pos);
				//main.newWindow.x = 1360;
				//main.newWindow.bounds = new Rectangle(0, 0, 1024, 768);

				switch(screen2pos)
				{
					case "right":
						main.moveRight();
						break;
					case "left":
						main.moveLeft();
						break;
					case "up":
						main.moveUp();
						break;
					case "down":
						main.moveDown();
						break;
					default:
						main.moveRight();
				}
				var currentScreen:Screen = getCurrentScreen();
				main.newWindow.bounds = currentScreen.bounds;
				_stage.displayState = StageDisplayState.FULL_SCREEN;
			}



			SwfLoader.SwfLoad(Assets.bg2Url,loaded);
		}
		public static var photoPlayer:PhotoPlayer;
		private static function loaded(e:Event=null):void
		{
			if(e && e.type == Event.COMPLETE)
			{
				var bg:Bitmap = e.target.content as Bitmap;
				if(bg)
				{
					bg.width = _stage.stageWidth;
					bg.height = _stage.stageHeight;
					_stage.addChildAt(bg,0);
				}
			}

			if(photoPlayer==null)
				photoPlayer = new PhotoPlayer(_stage.stageWidth,_stage.stageHeight);
			_stage.addChild(photoPlayer);
			photoPlayer.playIndex(0);
		}

		public static function resetPhotoPlayer():void
		{
			if(photoPlayer)
			{
				ViewSet.removes(photoPlayer);
				if(photoPlayer.parent){
					photoPlayer.parent.removeChild(photoPlayer);
				}
				photoPlayer = null;
			}
			photoPlayer = new PhotoPlayer(_stage.stageWidth,_stage.stageHeight);
			_stage.addChild(photoPlayer);
		}


		private function moveLeft():void{
			var currentScreen:Screen = getCurrentScreen();
			var left:Array = Screen.screens;
			left.sort(sortHorizontal);
			for(var i:int = 0; i < left.length - 1; i++){
				if(left[i].bounds.left < _stage.nativeWindow.bounds.left){
					_stage.nativeWindow.x +=
						left[i].bounds.left - currentScreen.bounds.left;
					_stage.nativeWindow.y += left[i].bounds.top - currentScreen.bounds.top;
				}
			}
		}
		private function moveRight():void{
			var currentScreen:Screen = getCurrentScreen();
			var left:Array = Screen.screens;
			left.sort(sortHorizontal);
			for(var i:int = left.length - 1; i > 0; i--){
				if(left[i].bounds.left > _stage.nativeWindow.bounds.left){
					_stage.nativeWindow.x +=
						left[i].bounds.left - currentScreen.bounds.left;
					_stage.nativeWindow.y += left[i].bounds.top - currentScreen.bounds.top;
				}
			}
		}
		private function moveUp():void{
			var currentScreen:Screen = getCurrentScreen();
			var top:Array = Screen.screens;
			top.sort(sortVertical);
			for(var i:int = 0; i < top.length - 1; i++){
				if(top[i].bounds.top < _stage.nativeWindow.bounds.top){
					_stage.nativeWindow.x += top[i].bounds.left - currentScreen.bounds.left;
					_stage.nativeWindow.y += top[i].bounds.top - currentScreen.bounds.top;
					break;
				}
			}
		}
		private function moveDown():void{
			var currentScreen:Screen = getCurrentScreen();
			var top:Array = Screen.screens;
			top.sort(sortVertical);
			for(var i:int = top.length - 1; i > 0; i--){
				if(top[i].bounds.top > _stage.nativeWindow.bounds.top){
					_stage.nativeWindow.x += top[i].bounds.left - currentScreen.bounds.left;
					_stage.nativeWindow.y += top[i].bounds.top - currentScreen.bounds.top;
					break;
				}
			}
		}
		private function sortHorizontal(a:Screen,b:Screen):int{
			if (a.bounds.left > b.bounds.left){
				return 1;
			} else if (a.bounds.left < b.bounds.left){
				return -1;
			} else {return 0;}
		}

		private function sortVertical(a:Screen,b:Screen):int{
			if (a.bounds.top > b.bounds.top){
				return 1;
			} else if (a.bounds.top < b.bounds.top){
				return -1;
			} else {return 0;}
		}
		private static function getCurrentScreen():Screen{
			var current:Screen;
			var screens:Array = Screen.getScreensForRectangle(_stage.nativeWindow.bounds);
			(screens.length > 0) ? current = screens[0] : current = Screen.mainScreen;
			return current;
		}
	}
}
